from manim import *



class math1(MovingCameraScene):
    def construct(self):
        self.camera.background_color = BLACK  # 设置背景颜色
        self.camera.frame_width = 100  # 设置边框宽度
        self.camera.frame_height = 60  # 设置边框高度
        self.camera.pixel_height = 1080  # 设置像素高度
        self.camera.pixel_width = 1920  # 设置像素宽度
        self.camera.center = ORIGIN  # 设置中心点位置
        self.camera.scale_factor = 1.0  # 设置缩放因子
        #设置横线
        for i in range(6*2+1):       
            dot1=Dot([-50,5*(i-6),0]).set_opacity(0.5)
            dot2=Dot([50,5*(i-6),0]).set_opacity(0.5)
            if i==6:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
                
            else:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
               
            self.add(dot1,dot2,line1)
        #设置竖线
        for i in range(10*2+1):        
            dot3=Dot([(i-10)*5,-30,0]).set_opacity(0.5)
            dot4=Dot([(i-10)*5,30,0]).set_opacity(0.5)
            if i==10:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
                
            else:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
            self.add(dot3,dot4,line2)
        #设置三个点
        dot1 = Dot(radius=1, color=RED)  
        dot1.move_to([-47.5,27.5,0]) 
        dot2 = Dot(radius=1, color=YELLOW)  
        dot2.move_to([-42.5,27.5,0])  
        dot3 = Dot(radius=1, color=GREEN)  
        dot3.move_to([-37.5,27.5,0])
        #镜头跟进效果 
        self.camera.frame.save_state()
        #题目出现
         # 第一个文本对象
        text = Tex(r"$\text{设 } f(x) \text{ 在 } x=a \text{ 的某领域内有定义，在 } x=a \text{ 的某去心领域内可导,}$",
                   tex_template=TexTemplateLibrary.ctex).scale(5).move_to([-10, 0, 0])

        # 第二个文本对象
        text0 = Tex(r"$\text{下述论断正确的是()}$",
                    tex_template=TexTemplateLibrary.ctex).scale(5).next_to(text, RIGHT, buff=1)

        # 第三个文本对象
        text1 = Tex(r"$\text{(A) 若 } \lim\limits_{x \to a} f'(x) = A \text{, 则 } f'(a) = A \text{}$",
                    tex_template=TexTemplateLibrary.ctex).scale(5).move_to([-22, -5, 0])
        text2 = Tex(
                    r"\text{(B)若}$f'\left( a\right) =A$\text{,则}$\lim\limits _{x\rightarrow a}f'\left( x\right) =A$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([22,-5,0])
        text3 = Tex(
                   r"\text{(C)若}$\lim\limits _{x\rightarrow a}f'\left( x\right) =\infty $\text{,则}$f'\left( a\right) $\text{不存在}"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-22,-12,0])
        text4 = Tex(
                    r"\text{(D)若}$f'\left( a\right) $\text{不存在}\text{,则}$\lim\limits _{x\rightarrow a}f'\left( x\right) =\infty $"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([22,-12,0])

        self.play(Write(text),self.camera.frame.animate.scale(0.8),Write(text1),Write(text2),Write(text3),Write(text4),Write(text0))
        self.wait(0.1)
        #镜头拉回
          #分析文本
        #text100=MarkupText("<b><i>分析/解：</i></b>",color=PINK).scale(5.0).move_to([-38,12.5,0])
        self.play(Restore(self.camera.frame))
        self.play(FadeIn(dot1),run_time=0.1)
        self.play(FadeIn(dot2),run_time=0.1)
        self.play(FadeIn(dot3),run_time=0.1)
        #题目向上移动
        self.play(text.animate().move_to([-10,20,0]),text1.animate().move_to([-22,15,0]) ,text2.animate().move_to([22,15,0]),text3.animate().move_to([-22,8,0]),text4.animate().move_to([22,8,0]) ,text0.animate().shift(20*UP) )
        self.wait(0.1)
         #半透明矩阵扩展
        rectangle=Rectangle(color=YELLOW,fill_opacity=0.5,width=1,height=2.8
                            ,stroke_color=YELLOW,stroke_width=0).move_to([-22,15,0])
        self.play(
            rectangle.animate.stretch(45.0,dim=0)
        )
        self.wait(0.1)

        text5 = Tex(
                   r"$f'\left( a\right) =\lim\limits _{x\rightarrow a}\dfrac{f\left( x\right) -f\left( a\right) }{x-a}===\lim\limits _{x\rightarrow a}f'\left( x\right) =A$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-10,-5,0])
        self.play(Create(text5))
        self.wait(0.1)
        text6 = Tex(
                   r"\text{洛}",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-5,-2,0])
        self.play(Create(text6))
        self.wait(0.1)
        #洛必达三个条件

        text8 = Tex(
                   r"}$1.\dfrac{0}{0}$\text{型}"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-22,-15,0])
        text9 = Tex(
                    r"\text{2.去心领域可导} "
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-22,-20,0])
        text10 = Tex(
                   r"\text{3.洛后极限三种情况：存在,∞,震荡}"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-22,-25,0])

        self.play(Create(text8),Create(text9),Create(text10))
        self.wait(0.1)

        rectangle2=Rectangle(color=BLUE,fill_opacity=0.0,width=45,height=11.5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle2.move_to([-22, -17, 0])
        self.play(Create(rectangle2))
        self.wait(0.1)

        text11 = Tex(
                   r"\text{存在性}",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([10,-17,0])
        self.play(Create(text11))
        self.wait(0.1)
        rectangle3=Rectangle(color=BLUE,fill_opacity=0.0,width=45,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle3.move_to([-22, -25, 0])
        self.play(Create(rectangle3))
        self.wait(0.1)

        text12 = Tex(
                   r"\text{有用性}",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([10,-25,0])
        self.play(Create(text12))
        self.wait(0.1)

       



        #框框
        rectangle1=Rectangle(color=BLUE,fill_opacity=0.0,width=16.5,height=5.5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle1.move_to([7.5, -5, 0])
        self.play(Create(rectangle1))
         #半透明矩阵扩展
        rectangle4=Rectangle(color=GREEN,fill_opacity=0.5,width=1,height=5
                            ,stroke_color=YELLOW,stroke_width=0).move_to([-22,-25,0])
        self.play(
            rectangle4.animate.stretch(45.0,dim=0)
        )
        self.wait(0.1)

        #曲线箭头

        #曲线箭头
        # 创建一个带有箭头的曲线，让箭头向上凸
        curved_arrow = CurvedArrow(
            start_point=[7, 0, 0],  # 起始点坐标
            end_point=[-15, 0, 0],  # 终止点坐标
            angle=PI/4,  # 弯曲的弧度，向上凸
            tip_shape=StealthTip,  # 箭头类型
            color=BLUE,
            stroke_width=20 
        )
         # 缩放箭头头部
        curved_arrow.tip.scale(3)
        
        # 添加到场景中并播放动画
        self.play(Create(curved_arrow))

        #文本

        text7 = Tex(
                   r"\text{传递？}",color=PURE_RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-5,5,0])
        self.play(Create(text7))
        self.wait(0.1)

        self.play(
            rectangle4.animate.move_to([-22,-15,0])
        )
        self.wait(0.1)
        #文本+镜头拉近
        text8 = Tex(
                   r"$\dfrac{?}{0}$",color=PURE_RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(10.0).move_to([30,-5,0])
        self.play(Create(text8),self.camera.frame.animate.scale(0.8).move_to([5,-5,0]))
        self.wait(0.1)
        #镜头拉回
        self.play(Restore(self.camera.frame))

        self.play(FadeOut(text8))
        self.wait(0.1)

        #透明框框变红
        self.play(
            rectangle4.animate.set_color(RED)
        )
        #清空部分元素
        self.play(Uncreate(text5),FadeOut(text6),FadeOut(rectangle1),FadeOut(rectangle)
                  ,Uncreate(text7),FadeOut(curved_arrow),FadeOut(rectangle4))
                  
        self.wait(0.1)   

        #分析B
         #半透明矩阵扩展
        rectangle5=Rectangle(color=YELLOW,fill_opacity=0.5,width=1,height=2.8
                            ,stroke_color=YELLOW,stroke_width=0).move_to([22,15,0])
        self.play(
            rectangle5.animate.stretch(45.0,dim=0)
        )
        self.wait(0.1)

        text9 = Tex(
                   r"$A=f'\left( a\right) =\lim\limits _{x\rightarrow a}\dfrac{f\left( x\right) -f\left( a\right) }{x-a}===\lim\limits _{x\rightarrow a}f'\left( x\right) $"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-2,-5,0])
        self.play(Create(text9))
        self.wait(0.1)
        text10 = Tex(
                   r"\text{洛}",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([10,-2,0])
        self.play(Create(text10))
        self.wait(0.1)

        ##框框标记

        rectangle6=Rectangle(color=BLUE,fill_opacity=0.0,width=20,height=8
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle6.move_to([-5, -5, 0])
        self.play(Create(rectangle6))

        #半透明矩阵扩展
        rectangle7=Rectangle(color=GREEN,fill_opacity=0.5,width=1,height=5
                            ,stroke_color=YELLOW,stroke_width=0).move_to([-22,-15,0])
        self.play(
            rectangle7.animate.stretch(45.0,dim=0)
        )
        self.wait(0.1)

        #半透明框框扩展

        rectangle8=Rectangle(color=BLUE,fill_opacity=0.5,width=1,height=5
                            ,stroke_color=YELLOW,stroke_width=0).move_to([12.5,20,0])
        self.play(
            rectangle8.animate.stretch(30.0,dim=0)
        )
        self.wait(0.1)
        self.play(FadeOut(rectangle8))
        self.wait(0.1)
        #半透明框框下移
        self.play(
            rectangle7.animate.move_to([-22,-20,0])
        )
        self.wait(0.1)
        #半透明框框下移+变红+?
        text11 = Tex(
                   r"\text{?}",color=PURE_RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(10.0).move_to([35,-5,0])

        self.play(
            rectangle7.animate.move_to([-22,-25,0]).set_color(RED),
            rectangle6.animate.move_to([20,-5,0]).set_color(RED).stretch(0.6,dim=0),
            Create(text11)


        )
        #清空部分

        self.play(Uncreate(text11),FadeOut(rectangle6),FadeOut(rectangle7),FadeOut(rectangle5)
                  ,Uncreate(text9),Uncreate(text10))
                  
        self.wait(0.1)   

        #C选项
        #半透明矩阵扩展
        rectangle9=Rectangle(color=YELLOW,fill_opacity=0.5,width=1,height=2.8
                            ,stroke_color=YELLOW,stroke_width=0).move_to([-22,8,0])
        self.play(
            rectangle9.animate.stretch(45.0,dim=0)
        )
        self.wait(0.1)

        text9 = Tex(
                   r"$f'\left( a\right) =\lim\limits _{x\rightarrow a}\dfrac{f\left( x\right) -f\left( a\right) }{x-a}===\lim\limits _{x\rightarrow a}f'\left( x\right)=\infty  $"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-2,-5,0])
        self.play(Create(text9))
        self.wait(0.1)
        text10 = Tex(
                   r"\text{洛}",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([5,-2,0])
        self.play(Create(text10))
        self.wait(0.1)

        #框框
        rectangle1=Rectangle(color=BLUE,fill_opacity=0.0,width=17.5,height=5.5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle1.move_to([17, -5, 0])
        self.play(Create(rectangle1))
         #半透明矩阵扩展
        rectangle4=Rectangle(color=GREEN,fill_opacity=0.5,width=1,height=5
                            ,stroke_color=YELLOW,stroke_width=0).move_to([-22,-25,0])
        self.play(
            rectangle4.animate.stretch(45.0,dim=0)
        )
        self.wait(0.1)

        #曲线箭头

        #曲线箭头
        # 创建一个带有箭头的曲线，让箭头向上凸
        curved_arrow = CurvedArrow(
            start_point=[17, 0, 0],  # 起始点坐标
            end_point=[-5, 0, 0],  # 终止点坐标
            angle=PI/4,  # 弯曲的弧度，向上凸
            tip_shape=StealthTip,  # 箭头类型
            color=BLUE,
            stroke_width=20 
        )
         # 缩放箭头头部
        curved_arrow.tip.scale(3)
        
        # 添加到场景中并播放动画
        self.play(Create(curved_arrow))

        #文本

        text11 = Tex(
                   r"\text{传递?}",color=PURE_RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([5,5,0])
        self.play(Create(text11))
        self.wait(0.1)

        self.play(
            rectangle4.animate.move_to([-22,-15,0])
        )
        self.wait(0.1)

        #两个箭头

        arrow1 = Arrow(start=[2,-12,0],end=[20,-11,0],color=RED
                       , stroke_width=20, tip_length=1)

        self.play(GrowArrow(arrow1))
         #文本
        text12 = Tex(
                   r"\text{是}",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([25,-11,0])
        self.play(Create(text12))
        self.wait(0.1)
        text14 = Tex(
                   r"$f'\left( a\right) =\infty $\text{(不存在)}",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text12,RIGHT,buff=2)
        self.play(Create(text14))
        self.wait(0.1)

        arrow2 = Arrow(start=[2,-17,0],end=[20,-18,0],color=RED
                       , stroke_width=20, tip_length=1)

        self.play(GrowArrow(arrow2))
        text13 = Tex(
                   r"\text{不是}",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([25,-18,0])
        self.play(Create(text13))
        self.wait(0.1)
        text15 = Tex(
                   r"$f'\left( a\right) $\text{不存在}",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text13,RIGHT,buff=2)
        self.play(Create(text15))
        self.wait(0.1)

        #清空部分

        self.play(Uncreate(text15),Uncreate(text12),Uncreate(text14),Uncreate(text13),Uncreate(text11),Uncreate(text10),Uncreate(text9)
                  ,Uncreate(arrow1),Uncreate(arrow2),Uncreate(curved_arrow)
                  ,FadeOut(rectangle1),FadeOut(rectangle4),FadeOut(rectangle9))
                  
        self.wait(0.1)   

        #D选项
        #半透明矩阵扩展
        rectangle9=Rectangle(color=YELLOW,fill_opacity=0.5,width=1,height=2.8
                            ,stroke_color=YELLOW,stroke_width=0).move_to([22,8,0])
        self.play(
            rectangle9.animate.stretch(45.0,dim=0)
        )
        self.wait(0.1)

        text9 = Tex(
                   r"$f'\left( a\right) =\lim\limits _{x\rightarrow a}\dfrac{f\left( x\right) -f\left( a\right) }{x-a}===\lim\limits _{x\rightarrow a}f'\left( x\right)  $"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-2,-5,0])
        self.play(Create(text9))
        self.wait(0.1)
        text10 = Tex(
                   r"\text{洛}",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([5,-2,0])
        self.play(Create(text10))
        self.wait(0.1)


        #框框
        rectangle10=Rectangle(color=BLUE,fill_opacity=0.0,width=29,height=8
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle10.move_to([-11, -5, 0])
        self.play(Create(rectangle10))
         #半透明矩阵扩展
        rectangle4=Rectangle(color=RED,fill_opacity=0.5,width=1,height=5
                            ,stroke_color=YELLOW,stroke_width=0).move_to([-22,-15,0])
        self.play(
            rectangle4.animate.stretch(45.0,dim=0)
        )
        self.wait(0.1)



        











        self.wait()